﻿#include "iplinkdialog.h"
#include "ui_iplinkdialog.h"
#include "qevent.h"
#include <QMessageBox>
#include <QRegExpValidator>
#include "myapp.h"
IpLinkDialog::IpLinkDialog(QWidget* parent) :
    QDialog(parent),
    ui(new Ui::IpLinkDialog)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::FramelessWindowHint );
    ui->led_ip->setValidator(new QRegExpValidator(QRegExp("^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
                             "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
                             "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
                             "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$")));
    ui->led_port->setValidator(new QIntValidator(1024, 65535));
    ui->led_ip->addItems(myApp::ips);
    this->setWindowTitle("直接连接");
}

IpLinkDialog::~IpLinkDialog()
{
    delete ui;
}

void IpLinkDialog::on_bt_close_clicked()
{
    this->close();
}

void IpLinkDialog::on_pushButton_clicked()
{

    if(ui->led_ip->currentText().isEmpty() || ui->led_port->text().isEmpty())
    {
        if(QMessageBox::warning(this, tr("警告"), tr("请将IP与端口填写完整"), QMessageBox::Ok, QMessageBox::NoButton))
        {
            return;
        }
    }
    if(ui->radioButton->isChecked())
    {
        emit linkwebsocket(ui->led_ip->currentText());
    }
    else
    {
        if(!myApp::ips.contains(ui->led_ip->currentText()))
            myApp::ips.append(ui->led_ip->currentText());
        emit linkwebsocket(QString("ws://%1:%2").arg(ui->led_ip->currentText()).arg(ui->led_port->text()));
    }
    this->accept();
}

void IpLinkDialog::mousePressEvent(QMouseEvent* e)
{
    if(e->button() == Qt::LeftButton)
        clickPos = e->pos();
}
void IpLinkDialog::mouseMoveEvent(QMouseEvent* e)
{
    if(e->buttons()&Qt::LeftButton)
    {
        if((e->pos() + pos() - clickPos) != QPoint(0, 0))
        {
            this->move(e->pos() + pos() - clickPos);
        }
    }
}
